<?php
require_once('model/forecastDays.class.php');

class PlaceModel {

	private $name;
	private $fromDate;
	private $dayIndex;
	private $hour;
	private $latitude;
	private $longitude;
	private $distanceFromStart;
	private $forecastDays;

	public function __construct($name, $latitude, $longitude, $distance = 0){
		$this->name = $name;
		$this->latitude = $latitude;
		$this->longitude = $longitude;
		$this->distanceFromStart = $distance;
	}

	public function getName(){ return utf8_encode($this->name); }
	public function getFromDate(){ return $this->fromDate; }
	public function getLatitude(){ return $this->latitude; }
	public function getLongitude(){ return $this->longitude; }
	public function getDistance(){ return $this->distanceFromStart; }
	public function getNiceDistance(){ return round($this->distanceFromStart); }
	public function getDayIndex(){ return $this->dayIndex; }
	public function getForecast($daysAhead, $starthour, $addDist = false){
		//	echo "<hr/>";
		//	echo $this->hour.'<br/>'.$starthour.'<br/>';
		$obj = $this->forecastDays;
		if ($addDist){
			if ($this->hour < $starthour){
				$daysAhead++;	// nytt døgn
			}
			return $obj->getHourForecast($daysAhead,$this->hour);
		}
		return $obj->getHourForecast($daysAhead,$starthour);
	}

	public function getForecastArray(){ return $this->forecastDays; }
	public function getHour(){ return $this->hour; }
	public function getNiceHour(){ 
	if($this->hour<10){
		if($this->hour==00){
			return $this->hour;
		}else{
			return '0'.$this->hour;
		}
	}else{
		return $this->hour; }
	}
	
	public function getNiceDate(){
		$date = $this->fromDate;
		$fromd = explode('T',$date);
		$date = new DateTime($fromd[0]);
		return $date->format("Y-m-d");
	}
	
	public function getSymbolString($symbol){
		$thisHour = $this->getHour();
		if($thisHour<8 || $thisHour>20){
			return '"'.'http://api.yr.no/weatherapi/weathericon/1.0/?symbol='.$symbol.';is_night=1;content_type=image/png;'.'"';
		}
		else{
			return '"'.'http://api.yr.no/weatherapi/weathericon/1.0/?symbol='.$symbol.';content_type=image/png;'.'"';
		}
	}

	public function setHour($param){ $this->hour = $param; }
	public function setName($param){ $this->name = $param; }
	public function setFromDate($param){ $this->fromDate = $param; }
	public function setDayIndex($param){ $this->dayIndex = $param; }
	public function setLatitude($param){ $this->latitude = $param; }
	public function setLongitude($param){ $this->longitude = $param; }
	public function setDistance($param){ $this->distanceFromStart = $param; }
	public function setForcastDays($param){ $this->forecastDays = $param; }

	public function generateDate($date, $dist){
		//m� legge til parameter for transportmiddel
		//Gjenomsnittshastiget som 60 for bil b�r lagres som konstant ?

		$fromdate = explode('T',$date);
		$date = new DateTime($fromdate[0]);

		$fromTime = explode(':',$fromdate[1]);
			
		$distTime = round(($dist / 60));
		$newTime = $distTime + $fromTime[0];
		if($newTime>23){
			$date->modify("+1 day");
//			$this->dayIndex++;
			$newTime = (($distTime + $fromTime[0])%24);
		}

		$this->hour = $newTime;

		$newDate =$date->format("Y-m-d").'T'.$newTime.':00:00Z';
		$this->fromDate = ($newDate);
		//return $newDate;
	}

	public function addHour($hour2){
		
		$oldHour = $this->hour;
		if(($oldHour+$hour2)>23){
			$date = new DateTime($this->getFromDate());
			$date->modify("+1 day");
			$this->dayIndex++ ;
			$this->fromDate = ($date->format("Y-m-d"));
			$this->hour = ($oldHour+$hour2)%24;
			
		 return 1;
		}else{
		//	echo "geie<br/>";
			$this->hour = ($oldHour+$hour2);
		return 0;	
		}
		
		 
		
	}
	
	public function getNumber($n1,$n2,$n3,$n4,$n5){
		$smaal = $n1;
		if($n2<$smaal){$smaal = $n2;}
		if($n3<$smaal){$smaal = $n3;}
		if($n4<$smaal){$smaal = $n4;}
		if($n5<$smaal){$smaal = $n5;}
		return $smaal;
	}
}


?>